<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 2018/8/19
 * Time: 10:46
 */
namespace app\admin\command;
use app\api\controller\model\WechatModel;
use app\api\controller\model\XcxPageModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
class TimeTask extends Command
{
    protected function configure(){
        $this->setName('TimeTask')->setDescription("计划任务:自动关闭订单/生成商品拼团信息");
    }

    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/
        try {
            //活动即将开始提醒
            $this->activitySoonStart($output);
            //会员到期通知
            $this->vipExpireNotice($output);
        } catch (\Exception $e) {
            $output->writeln('ERROR');
            $output->writeln($e->getMessage());
            Log::error('执行定时任务失败');
            Log::error($e->getMessage());
        }
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }

    private function vipExpireNotice($output){
        //一个月前通知
        $month_vip_list = db("gzns_user_vip_level uvl")
            ->join("gzns_vip_level vl","vl.id = uvl.vip_level_id","LEFT")
            ->field('uvl.*,vl.name')
            ->where("end_time","<",date("Y-m-d H:i:s", strtotime("+1 months",time())))
            ->where("month_notice",0)
            ->select();
        foreach ($month_vip_list as $month){
            db("gzns_user_vip_level")->where("id",$month["id"])->update([
                "month_notice" => 1
            ]);
            //发送模板消息 核销提醒
            $third_account = db("gzns_user_third_account")->where("user_id",$month["user_id"])->where("third_type",1)->find();
            if($third_account){
                $wechatModel = new WechatModel(1, false);
                $aptitude_data = [
                    "会员类型:".$month["name"], //服务名称
                    $month["start_time"], //开始时间
                    $month["end_time"], //结束时间
                    "您的会员还有一个月就到期，请注意续费哦！", //温馨提示
                ];
                $wechatModel->vipExpireNotice($month["user_id"],$third_account["third_open_id"],$aptitude_data,XcxPageModel::$VIP_DETAIL);
            }
        }

        $week_vip_list = db("gzns_user_vip_level uvl")
            ->join("gzns_vip_level vl","vl.id = uvl.vip_level_id","LEFT")
            ->field('uvl.*,vl.name')
            ->where("end_time","<",date("Y-m-d H:i:s", strtotime("+1 week",time())))
            ->where("week_notice",0)
            ->select();
        foreach ($week_vip_list as $week){
            db("gzns_user_vip_level")->where("id",$week["id"])->update([
                "week_notice" => 1
            ]);
            //发送模板消息 核销提醒
            $third_account = db("gzns_user_third_account")->where("user_id",$week["user_id"])->where("third_type",1)->find();
            if($third_account){
                $wechatModel = new WechatModel(1, false);
                $aptitude_data = [
                    "会员类型:".$week["name"], //服务名称
                    $week["start_time"], //开始时间
                    $week["end_time"], //结束时间
                    "您的会员还有一周就到期，请注意续费哦！", //温馨提示
                ];
                $wechatModel->vipExpireNotice($week["user_id"],$third_account["third_open_id"],$aptitude_data,XcxPageModel::$VIP_DETAIL);
            }
        }

        $day_vip_list = db("gzns_user_vip_level uvl")
            ->join("gzns_vip_level vl","vl.id = uvl.vip_level_id","LEFT")
            ->field('uvl.*,vl.name')
            ->where("end_time","<",date("Y-m-d H:i:s", strtotime("+1 day",time())))
            ->where("day_notice",0)
            ->select();
        foreach ($day_vip_list as $day){
            db("gzns_user_vip_level")->where("id",$day["id"])->update([
                "day_notice" => 1
            ]);
            //发送模板消息 核销提醒
            $third_account = db("gzns_user_third_account")->where("user_id",$day["user_id"])->where("third_type",1)->find();
            if($third_account){
                $wechatModel = new WechatModel(1, false);
                $aptitude_data = [
                    "会员类型:".$day["name"], //服务名称
                    $day["start_time"], //开始时间
                    $day["end_time"], //结束时间
                    "您的会员还有一天就到期，请注意续费哦！", //温馨提示
                ];
                $wechatModel->vipExpireNotice($day["user_id"],$third_account["third_open_id"],$aptitude_data,XcxPageModel::$VIP_DETAIL);
            }
        }

    }

    private function activitySoonStart($output){
        $setting = db("gzns_setting")->field("notice_time,time_type")->find();
        if($setting["time_type"] == 1){//分钟
            $time = time() + $setting["notice_time"] * 60;
        }else{//天
            $time = time() + $setting["notice_time"] * 24 * 60 * 60;
        }
        $course_list = db("gzns_course c")->field("c.*,t.name teacher_name")
            ->join("gzns_teacher t","t.id = c.teacher_id","LEFT")
            ->where("c.type",3)
            ->where("c.end_time","<",date("Y-m-d H:i:s",$time))
            ->where("c.is_notice",0)
            ->select();
        foreach ($course_list as $course){
            db("gzns_course")->where("id",$course["id"])->update([
                "is_notice" => 1
            ]);
            $order_list = db("gzns_order o")->field("o.id,o.user_id")
                ->join("gzns_order_item oi","oi.order_id = o.id","LEFT")
                ->where("o.order_type",2)
                ->where("oi.item_id",$course["id"])
                ->select();
            foreach ($order_list as $order){
                //发送模板消息 核销提醒
                $third_account = db("gzns_user_third_account")->where("user_id",$order["user_id"])->where("third_type",1)->find();
                if($third_account){
                    $wechatModel = new WechatModel(1, false);
                    $page_url =  XcxPageModel::$OFFLINE_COURSE_DETAIL."?order_id=".$order["id"];
                    $aptitude_data = [
                        $course["name"], //课程名称
                        $course["teacher_name"], //讲师
                        $course["address"].$course["short_address"], //地址
                        $course["time"].'-'.$course["end_time"], //上课时间
                        '您购买的线下课程即将开始，请注意及时参与哦！', //备注
                    ];
                    $wechatModel->activitySoonStart($order["user_id"],$third_account["third_open_id"],$aptitude_data,$page_url);
                }
            }

        }
    }
}